package com.woniu.woniu_bx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.woniu_bx.mapper.MenuMapper;
import com.woniu.woniu_bx.pojo.Menu;
import com.woniu.woniu_bx.pojo.Perm;
import com.woniu.woniu_bx.service.MenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:GuoHai
 * @program: IntelliJ IDEA
 * @data: 2022/8/27 16:20
 */
@Service
@Transactional
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    //根据角色id查询其所有菜单（未分好父子层级）
    @Override
    public List<Menu> findMenusByRoleId(int id) {
        List<Menu> menus = menuMapper.getByRoleId(id);
        return menus;
    }

    //查询所有的菜单（已经分好父子层级）
    @Override
    public List<Menu> menusList() {
        List<Menu> menus = menuMapper.findAll();
        for(int i = 0 ; i < menus.size() ; i ++){
            List<Menu> subMenus = new ArrayList<Menu>();
            if(menus.get(i).getPid()==null){
                for(int j = 0 ; j < menus.size() ; j++){
                    if(menus.get(j).getPid()==menus.get(i).getId()){
                        subMenus.add(menus.get(j));
                        menus.remove(j);
                        j--;
                    }
                }
            }
            menus.get(i).setSubMenu(subMenus);
        }
        return menus;
    }

    @Override
    public List<Menu> getMenuListByRole(int id) {
        return menuMapper.getByRoleId(id);
    }
}
